//
//  AddAddressViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/9.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "AddAddressViewController.h"
#import "SkyAssociationMenuView.h"
#import "ScroButton.h"
@interface AddAddressViewController ()<SkyAssociationMenuViewDelegate>
@property(nonatomic,strong)SkyAssociationMenuView * menuView;
@property(nonatomic,strong)NSMutableArray * captionArray;
@property(nonatomic,strong)NSMutableArray * cityArray;
@property(nonatomic,strong)NSMutableArray * sectionArray;
@property(nonatomic,strong)NSString * captionId;
@property(nonatomic,strong)NSString * cityId;
@property(nonatomic,strong)NSString * sectionId;
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UILabel *captionLabel;
@property (strong, nonatomic) IBOutlet UILabel *cityLabel;
@property (strong, nonatomic) IBOutlet UILabel *sectionLabel;
@property (strong, nonatomic) IBOutlet UITextField *zipCodeField;//邮编
@property (strong, nonatomic) IBOutlet UITextField *addressField;//地址
@property (strong, nonatomic) IBOutlet UITextField *nameField;//姓名
@property (strong, nonatomic) IBOutlet UITextField *phoneField;//电话
@property (strong, nonatomic) IBOutlet UIButton *cancleButton;

@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (strong, nonatomic) IBOutlet UILabel *headTitle;
@property(nonatomic,strong)NSString * addressId;
@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _captionArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    _cityArray = [NSMutableArray array];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:@"select * from sys_area where a_level = 3"];
    for (CityArea *area  in cityArr) {
        [_captionArray addObject:area.a_name];
    }
    [self initViewLayout];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.navView];

    _saveButton.layer.masksToBounds = YES;
    _saveButton.layer.cornerRadius = 4*VerticalRatio();
    
    _menuView = [[SkyAssociationMenuView alloc]initWithFrame:flexibleFrame(CGRectMake(90, 113, 230, 200), NO) lastClassTitle:@"返回上一级"];
    _menuView.delegate = self;

    
    if (_addressInfo) {//编辑
        [_cancleButton setTitle:@"删除" forState:UIControlStateNormal];
        _headTitle.text = @"编辑收货地址"; 
        _zipCodeField.text = _addressInfo[@"msadEmail"];
        _captionId =_addressInfo[@"msadProvinceId"];
        _captionLabel.text = _addressInfo[@"msadProvinceName"];
        _cityId = _addressInfo[@"msadCityId"];
        _cityLabel.text = _addressInfo[@"msadCityName"];
        _sectionId = _addressInfo[@"msadAreaId"];
        _sectionLabel.text = _addressInfo[@"msadAreaName"];
        _nameField.text = _addressInfo[@"msadReceiverName"];
        _phoneField.text = _addressInfo[@"msadMobileNo"];
        _addressField.text = [NSString stringWithFormat:@"%@",_addressInfo[@"msadAddr"]];

    }else{//添加
        [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    }

}
- (IBAction)back:(id)sender {
    [self backBtn];
}

- (IBAction)choseAddress:(id)sender {
    [_menuView showAsDrawDownView];
}
#pragma mark === 保存地址
- (IBAction)saveAddress:(id)sender {
    

    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    
    }
    
    if ([Common dataIsNULL:_sectionLabel.text]||[Common dataIsNULL:_cityLabel.text]||[Common dataIsNULL:_sectionLabel.text]) {
        [CWHUDTips showTips:@"请选择所在区域"];
        return;
    }
  
    
    if ([Common dataIsNULL:_zipCodeField.text]) {
        [CWHUDTips showTips:_zipCodeField.placeholder];
        return;
    }
    
    if ([Common dataIsNULL:_addressField.text]) {
        [CWHUDTips showTips:_addressField.placeholder];
        return;
    }

    
    if ([Common dataIsNULL:_nameField.text]) {
        [CWHUDTips showTips:_nameField.placeholder];
        return;
    }
    if ([Common dataIsNULL:_phoneField.text]) {
        [CWHUDTips showTips:_phoneField.placeholder];
        return;
    }
           NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        NSString * lan = [InternationalControl userLanguage];
        if ([lan isEqualToString:@"en"]) {
            [dic setObject:@"2" forKey:@"language"];
        }else{
            [dic setObject:@"1" forKey:@"language"];
        }
        [dic setObject:_captionId forKey:@"msadProvinceId"];
        [dic setObject:_cityId forKey:@"msadCityId"];
        [dic setObject:_sectionId forKey:@"msadAreaId"];
        [dic setObject:_captionLabel.text forKey:@"msadProvinceName"];
        [dic setObject:_cityLabel.text forKey:@"msadCityName"];
        [dic setObject:_sectionLabel.text forKey:@"msadAreaName"];
        [dic setObject:_zipCodeField.text forKey:@"msadEmail"];
        [dic setObject:_addressField.text forKey:@"msadAddr"];
        [dic setObject:_nameField.text forKey:@"msadReceiverName"];
        [dic setObject:_phoneField.text forKey:@"msadMobileNo"];
    if (_addressInfo) {
        [dic setObject:_addressInfo[@"msadId"] forKey:@"msadId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:GS_updateMallShippingAddress withHttpType:@"POST" withTag:GS_GS_updateMallShippingAddress];
    }else{
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:GS_addMallShippingAddress withHttpType:@"POST" withTag:GS_GS_addMallShippingAddress];
    }

}
#pragma mark === 删除地址
- (IBAction)deleteAddress:(UIButton *)sender {
    if (_addressInfo) {//编辑删除
        if (![Common getLoginStatus]) {
            [[AppDelegate sharedAppDelegate]ShowLoginPage];
            return;
        }
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:_addressInfo[@"msadId"] forKey:@"ids"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:GS_deleteMallShippingAddress withHttpType:@"POST" withTag:GS_GS_deleteMallShippingAddress];
    }else{//取消添加
        if (_menuView.superview) {
            [_menuView dismiss];
        }else{
            [self backBtn];
        }
    }

}

- (NSInteger)assciationMenuView:(SkyAssociationMenuView*)asView countForClass:(NSInteger)idx {
    if (idx == 0) {
        return _captionArray.count;
    }else if (idx == 1){
        return _cityArray.count;
    }else{
        return _sectionArray.count;
    }
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 {
    
    return _captionArray[idx_1];
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 class_2:(NSInteger)idx_2 {
    
    return _cityArray[idx_2];
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 class_2:(NSInteger)idx_2 class_3:(NSInteger)idx_3 {
    return _sectionArray[idx_3];
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1{
    _captionLabel.text = _captionArray[idx_1];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_captionArray[idx_1] ]];
    CityArea *area = cityArr[0];
    NSArray *areaArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %d",area.a_id]];
    _captionId = [NSString stringWithFormat:@"%d",area.a_id];
    [_cityArray removeAllObjects];
    for (CityArea *area  in areaArr) {
        [_cityArray addObject:area.a_name];
    }
    return YES;
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1 class2:(NSInteger)idx_2{
    _cityLabel.text = _cityArray[idx_2];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_cityArray[idx_2]]];
    CityArea *area = cityArr[0];
    NSArray *areaArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %d",area.a_id]];
    _cityId = [NSString stringWithFormat:@"%d",area.a_id];
    [_sectionArray removeAllObjects];
    for (CityArea *area  in areaArr) {
        [_sectionArray addObject:area.a_name];
    }
    
    return YES;
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1 class2:(NSInteger)idx_2 class3:(NSInteger)idx_3{
    _sectionLabel.text = _sectionArray[idx_3];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_sectionArray[idx_3]]];
    CityArea *area = cityArr[0];
    _sectionId = [NSString stringWithFormat:@"%d",area.a_id];
    
    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 50) {
        if (buttonIndex == 0) {
            [self backBtn];
        }else{
            NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
            [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
            [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
            [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
            [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
            [dic setObject:_addressId forKey:@"msadId"];
            self.shareApi.delegate=self;
            [self.shareApi  requestOperationWithDic:dic withPath:GS_setDefaultMallShippingAddress withHttpType:@"POST" withTag:GS_GS_setDefaultMallShippingAddress];
        }

    }else{
        if (buttonIndex == 0) {
            _endEditBlock(@"0");
            [self backBtn];
        }else{
            NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
            [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
            [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
            [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
            [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
            [dic setObject:_addressInfo[@"msadId"] forKey:@"msadId"];
            self.shareApi.delegate=self;
            [self.shareApi  requestOperationWithDic:dic withPath:GS_setDefaultMallShippingAddress withHttpType:@"POST" withTag:GS_GS_setDefaultMallShippingAddress];
        }
    }
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==GS_GS_addMallShippingAddress) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
//            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _addressId =[NSString stringWithFormat:@"%@",database[@"result"][@"msadId"]] ;
          UIAlertView * alertView =  [[UIAlertView alloc]initWithTitle:@"提示" message:@"添加地址成功,是否设为默认地址!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alertView.tag = 50;
            [alertView show];
        }
    }else if (tag == GS_GS_setDefaultMallShippingAddress){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            if (_addressInfo) {
                  _endEditBlock(@"0");
            }
            [self backBtn];
        }
    }else if (tag == GS_GS_deleteMallShippingAddress){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
           
            _endEditBlock(@"1");
            [self backBtn];
        }

    }else if (tag == GS_GS_updateMallShippingAddress){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
//            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            UIAlertView * alertView =  [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改地址成功,是否设为默认地址!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alertView.tag = 51;
            [alertView show];
           
        }

    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        [self dismissWaitDialog];
        
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
